import NumItem from "./NumItem";
import Utils from "./utils/Utils";

/**
 * 游戏数据
 */
export default class GameData {

  /**pay */
  public static payType: number = 0;
  /**飘钱类型 0卡通 1真钱 */
  public static gameType: number = 0;
  /**每次增加的金币 */
  public static addUsdCount: number = 60;
  /**球的时间s */
  public static ballTime: number = 3;


  /**游戏最大次数 */
  public static maxCount: number = 2;
  /**
   *  13的卡通美元
   *  14真美元
   */
  public static mapData: NumData[] = [
    { type: 1, num: 15, state: 1 },
    { type: 2, num: 23, award: 1 },
    { type: 3, num: 42 },
    { type: 4, num: 49 },
    { type: 5, num: 67 },
    { type: 1, num: 11 },
    { type: 2, num: 25, state: 1 },
    { type: 3, num: 34 },
    { type: 4, num: 55 },
    { type: 5, num: 64 },
    { type: 1, num: 6 },
    { type: 2, num: 19 },
    { type: 3, num: 40, state: 1 },
    { type: 4, num: 51, award: 2 },
    { type: 5, num: 61 },
    { type: 1, num: 5 },
    { type: 2, num: 21, state: 1, award: 2 },
    { type: 3, num: 35 },
    { type: 4, num: 58, state: 1, award: 2 },
    { type: 5, num: 70 },
    { type: 1, num: 10, state: 1 },
    { type: 2, num: 16, state: 1 },
    { type: 3, num: 38, state: 1, award: 2 },
    { type: 4, num: 48 },
    { type: 5, num: 66 },
  ]


  /**出球 */
  public static ballArray: NumData[] = [
    { type: 5, num: 67 },
    { type: 4, num: 55 },
    { type: 4, num: 48 },
    { type: 5, num: 66 },
  ]

  /**顶部字母 */
  public static ZiMuString: string[] = [
    "B", "I", "N", "G", "O"
  ]

  /**是否bingo */
  public static isBingo(nodeArr: cc.Node[]) {
    let arr: cc.Node[][] = Utils.arrTrans2(nodeArr, 5);

    let resultArr: cc.Node[] = [];
    //横
    for (let i: number = 0; i < 5; i++) {
      let b: boolean = arr[i].every((node: cc.Node) => {
        let num: NumItem = node.getComponent(NumItem);
        return num.state >= 1;
      })
      if (b) {//一排有五个

        for (let j: number = 0; j < 5; j++) {
          let node: cc.Node = arr[i][j];
          if (resultArr.indexOf(node) == -1) {
            resultArr.push(node);
          }
        }
      }
    }
    //竖
    let count: number = 0;
    for (let i: number = 0; i < 5; i++) {
      count = 0;
      for (let j: number = 0; j < 5; j++) {
        let node: cc.Node = arr[j][i];
        let num: NumItem = this.getNodeNumItem(node);
        if (num.state >= 1) {
          count++;
        }
      }
      if (count >= 5) {
        for (let j: number = 0; j < 5; j++) {
          let node: cc.Node = arr[j][i];
          if (resultArr.indexOf(node) == -1) {
            resultArr.push(node);
          }
        }
      }
    }

    count = 0;
    for (let i: number = 0; i < 5; i++) {
      let node: cc.Node = arr[i][i];
      let num: NumItem = this.getNodeNumItem(node);
      if (num.state >= 1) {
        count++;
      }
    }
    if (count >= 5) {
      for (let i: number = 0; i < 5; i++) {
        let node: cc.Node = arr[i][i];
        if (resultArr.indexOf(node) == -1) {
          resultArr.push(node);
        }
      }
    }

    count = 0;
    for (let i: number = 0; i < 5; i++) {
      let node: cc.Node = arr[4 - i][i];
      let num: NumItem = this.getNodeNumItem(node);
      if (num.state >= 1) {
        count++;
      }
    }
    if (count >= 5) {
      for (let i: number = 0; i < 5; i++) {
        let node: cc.Node = arr[4 - i][i];
        if (resultArr.indexOf(node) == -1) {
          resultArr.push(node);
        }

      }
    }

    resultArr = resultArr.filter((node: cc.Node) => {
      let num: NumItem = node.getComponent(NumItem);
      if (num.state == 1) {
        return true;
      } else {
        return false;
      }
    })
    return resultArr;
  }

  /**
   * 获取节点的num组件
   */
  public static getNodeNumItem(node: cc.Node) {
    return node.getComponent(NumItem);
  }


  /**
   * 判断是否存在了
   */
  public static isExist(result, node: cc.Node) {
    return node.getComponent(NumItem);
  }


  /**查找指定数据 */
  public static findIndex(arr: NumData[], num: number) {
    let len: number = arr.length;
    for (let i: number = 0; i < len; i++) {
      if (arr[i].num == num) {
        return i;
      }
    }
  }


  /**本地化配置 */
  public static languageConfig: languageData;
  public static languageIndex: number;



  /**系统语言初始化 */
  public static languageInit() {
    let languageIndex: number = 0;
    let language: string = cc.sys.language ;  
    if (language.indexOf("ja") >= 0) {//日语（Japanese） 
      languageIndex = 1;
    } else if (language.indexOf("de") >= 0) {//德语 (German)  
      languageIndex = 2;
    } else if (language.indexOf("fr") >= 0) {//法语（French）  
      languageIndex = 3;
    } else if (language.indexOf("ru") >= 0) {//俄语 (Russian)  
      languageIndex = 4;
    } else if (language.indexOf("es") >= 0) {//西班牙语 (Spanish)   
      languageIndex = 5;
    } else if (language.indexOf("it") >= 0) {//意大利语（Italian） 
      languageIndex = 6;
    } else if (language.indexOf("uk") >= 0) {//乌克兰语(Ukrainian)  
      languageIndex = 7;
    } else if (language.indexOf("pt") >= 0) {//葡萄牙语 (Portuguese) 
      languageIndex = 8;
    } else if (language.indexOf("id") >= 0) {//印度尼西亚语 (Indonesian)  in id
      languageIndex = 9;
    } else if (language.indexOf("hi") >= 0) {//印地语(Hindi) 
      languageIndex = 10;
    } else if (language.indexOf("vi") >= 0) {//越南语(Vietnamese)
      languageIndex = 11;
    } else if (language.indexOf("th") >= 0) {//泰语(Thai)
      languageIndex = 12;
    } else if (language.indexOf("ms") >= 0) {//马来语 (Malay)
      languageIndex = 13;
    } else if (language.indexOf("ro") >= 0) {//罗马尼亚语 (Romanian) 
      languageIndex = 14;
    } else if (language.indexOf("ce") >= 0) {//菲律宾语（Tagalog）
      languageIndex = 15;
    } else if (language.indexOf("ko") >= 0) {//韩语（Korean）
      languageIndex = 16;
    } else {
      languageIndex = 0;
    }
    GameData.languageIndex = languageIndex;
  }



}


export interface languageData {
  Youwon: string,
  Cashout: string,
  CONGRATULATIONS: string,
  moneyCount: number,
  unit: string,
  /**平台提示语 */
  hintString: string,
}

export interface NumData {
  /**类型 */
  type: number;
  /**数字 */
  num: number;
  /**状态 */
  state?: number;
  /**奖励 */
  award?: number;
}